package com.hyt.it.ogt.download.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.it.ogt.download.entity.Student;
import com.hyt.it.ogt.download.enums.BizStatusCode;
import com.hyt.it.ogt.download.enums.SearchType;
import com.hyt.it.ogt.download.service.IVideoService;
import com.hyt.it.ogt.download.service.impl.UserServiceImpl;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "1.视频下载接口", value = "视频下载接口")
@ApiSort(value = 1)
@Slf4j
@RestController
@RequestMapping("/video")
public class VideoController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private IVideoService iVideoService;

    @ApiOperation(value = "1.1 查询视频列表", notes = "返回CODE码：20000-成功，10010101-参数不合法,10010102-token已过期，请重新登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "考试ID", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true),
            @ApiImplicitParam(name = "searchText", value = "查询内容", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = false),
            @ApiImplicitParam(name = "searchType", value = "查询类型：all-全部，name-按姓名，zkz-按准考证号",
                    dataType = DataType.STRING, paramType = ParamType.QUERY, required = true),
            @ApiImplicitParam(name = "current", value = "当前页", dataType = DataType.INT,
                    paramType = ParamType.QUERY, required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条", dataType = DataType.INT,
                    paramType = ParamType.QUERY, required = true)
    })
    @GetMapping("list")
    public ApiResponse<Object> getVideoList(@RequestParam String token, @RequestParam String code,
                                            @RequestParam(required = false) String searchText,
                                            @RequestParam String searchType, @RequestParam Integer current,
                                            @RequestParam Integer pageSize) {
        log.info("视频下载服务getVideoList获取视频列表参数:{},{},{},{},{},{}", token, code, searchText, searchType, current, pageSize);
        if ((SearchType.SEARCH_BY_NAME.getCode().equalsIgnoreCase(searchType)
                || SearchType.SEARCH_BY_ADMISSION_NUMBER.getCode().equalsIgnoreCase(searchType))
                && StringUtils.isBlank(searchText)) {
            return ApiResponse.builder().code(BizStatusCode.VIDEO_PARAM_WRONG.getCode())
                    .data(BizStatusCode.VIDEO_PARAM_WRONG.getMsg()).build();
        }
        if (current > 0 && pageSize > 0) {
            String key = StringUtils.joinWith(":", UserServiceImpl.MONITOR_USER_TOKEN, token);
            Object officeId = redisTemplate.opsForValue().get(key);
            if (officeId == null) {
                return ApiResponse.builder().code(BizStatusCode.TOKEN_INVALIDATION.getCode())
                        .data(BizStatusCode.TOKEN_INVALIDATION.getMsg()).build();
            }
            IPage<Student> videoList = iVideoService.getVideoList(officeId.toString(), code, searchText, searchType, current, pageSize);
            return ApiResponse.builder().code(BizStatusCode.SUCCESS_CODE.getCode()).data(videoList).build();
        } else {
            return ApiResponse.builder().code(BizStatusCode.VIDEO_PARAM_WRONG.getCode())
                    .data(BizStatusCode.VIDEO_PARAM_WRONG.getMsg()).build();
        }
    }
}
